package com.zcsy.manage.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CellRegion;
import com.zcsy.manage.persistence.base.CellRegionDao;
import com.zcsy.manage.service.base.ICellBuildingService;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.base.ICellRegionService;
import com.zcsy.manage.service.base.ICellUnitService;
import com.zcsy.manage.service.system.impl.SystemComService;

/**
 * @Title: CellRegionService.java
 * @Package： com.zcsy.manage.service.base.impl
 * @Description: 小区区域接口实现类
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年4月6日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class CellRegionService extends SystemComService implements ICellRegionService {

    @Autowired
    private CellRegionDao cellRegionDao;

    @Autowired
    private ICellBuildingService cellBuildingService;
    
    @Autowired
    private ICellUnitService cellUnitService;
    
    @Autowired
    private ICellHouseService cellHouseService;
    
    @Autowired
    private ICellInfoService cellInfoService;
    
    @Override
    public int insert(CellRegion cellRegion) {
        
        // 插入一条区域，将当前区域所在的小区绑定依赖关系，有依赖的小区不能被删除
        CellInfo cellInfo = cellInfoService.selectByPrimaryKey(cellRegion.getCellId());
        cellInfo.setIsUsed(Constants.IS_USED);
        cellInfoService.update(cellInfo);
        
        return cellRegionDao.insert(cellRegion);
    }

    @Override
    public int update(CellRegion cellRegion) {
        CellRegion region = selectByPrimaryKey(cellRegion.getId());

        //如果区域名称修改了，则同步其下的所有楼栋、单元、房屋的区域名称
        if (!(region.getName()).equals(cellRegion.getName())) {
            cellBuildingService.updateById(cellRegion.getId(), cellRegion.getName(), Constants.REGION_TYPE);
            cellUnitService.updateById(cellRegion.getId(), cellRegion.getName(), Constants.REGION_TYPE);
            cellHouseService.updateById(cellRegion.getId(), cellRegion.getName(), Constants.REGION_TYPE);
        }
        
        region.setName(cellRegion.getName());
        region.setSort(cellRegion.getSort());
        region.setUserAccount(cellRegion.getUserAccount());
        region.setUpdateTime(cellRegion.getUpdateTime());
        
        return cellRegionDao.update(region);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return cellRegionDao.deleteByPrimaryKey(id);
    }

    @Override
    public CellRegion selectByPrimaryKey(String id) {
        return cellRegionDao.selectByPrimaryKey(id);
    }

    @Override
    public List<CellRegion> selectByKeyword(CellRegion cellRegion) {
        return cellRegionDao.selectByKeyword(cellRegion);
    }

    @Override
    public List<CellRegion> getAllCellRegion() {
        return cellRegionDao.getAllCellRegion();
    }

    @Override
    public List<CellRegion> getCellRegionByCellId(String cellId) {
        return cellRegionDao.getCellRegionByCellId(cellId);
    }

    @Override
    public int updateById(String id, String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        return cellRegionDao.updateById(map);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.base.ICellRegionService#selectForImport(com.zcsy.manage.model.base
     * .CellRegion)
     */
    @Override
    public List<CellRegion> selectForImport(CellRegion cellRegion) {
        return cellRegionDao.selectForImport(cellRegion);
    }

	@Override
	public int updateSort(CellRegion record) {
		return cellRegionDao.updateSort(record);
	}

    @Override
    public boolean isNameExists(String regionId, String cellId, String name) {
        return cellRegionDao.isNameExists(regionId, cellId, name);
    }

}
